<?php
/**
 * 商品Widget
 * 用于调用商品相关信息
 * @version 2014122412
 * @author Max.Yu <max@jipu.com>
 */

namespace Home\Widget;

use Home\Model\ItemModel;
use Think\Db;

class ItemWidget extends BaseWidget{

  /**
   * 调用商品列表
   * @param $order 排序
   * @param $limit 调用数量
   * @author Max.Yu <max@jipu.com>
   */
  public function itemLists($where, $field = true, $order = '`is_top` DESC, `sort` ASC', $limit = 10){
    $item_model = D('Item');
    $field = 'id, name, subname, price, mprice, buynum, is_top, sort, is_recommend, is_hot, is_promote, thumb, status';
    $order = '`is_top` DESC, `sort` ASC';
    //获取首页商品
    $data['lists'] = $item_model->lists($where, $field, $order, $limit);
    //模板输出变量赋值
    $this->data = $data;
    $this->display('Widget/itemLists');
  }

  /**
   * 调用商品列表
   * @param $order 排序
   * @param $limit 调用数量
   * @author Max.Yu <max@jipu.com>
   * {:W('Item/itemListsByCate', array('11, 1, 3, 14, 2, 18, 10', '', '', 16))}
   */
  public function itemListsByCate($cids = '', $field = true, $order = '`is_top` DESC, `sort` ASC', $limit = 10){
    //获取首页商品
    if($cids){
      $item_model = D('Item');
      $field = 'id, cid_1, name, subname, summary, price, mprice, buynum, is_top, sort, is_recommend, is_hot, is_promote, thumb, status';
      $order = '`is_top` DESC, `sort` ASC';
      $item_lists = array();
      $where = array(
          'id' => array('IN', $cids),
        //'pid' => 0
      );
      $lists = D('ItemCategory')->lists($where, 'id, name, ename', 'field(id, '.$cids.') ASC');
      $no_list_key=array();
      if($lists && is_array($lists)){
        foreach($lists as $key => &$value){
          $map = array(
              'cid_1|cid_2|cid_3' => $value['id'],
              'status' => array('gt', '0'), //下架商品不显示
              'is_recommend' => 1
          );
          $count = $item_model->where($map)->count();
          $value['item_lists'] = $item_model->lists($map, $field, $order, $limit <= $count ? $limit : 8);
          if(empty($value['item_lists']))$no_list_key[]=$key;
        }
      }
      foreach($no_list_key as $v)unset($lists[$v]);
      $this->lists = $lists;
    }
    $this->display('Widget/itemListsByCate');
  }

  /**
   * 调用商品列表--暂时弃用
   * @param array $cid_setting array(1=>20) 代表1分类20条数据
   * @author Max.Yu <max@jipu.com>
   * {:W('Item/itemListsByCate', array('11, 1, 3, 14, 2, 18, 10', '', '', 16))}
   */
  public function indexItemList($cid_setting = array()){

    //获取首页商品
    if($cid_setting){

      $item_model = D('Item');
      $field = 'id, cid_1, name, subname, summary, price, mprice, buynum, is_top, sort, is_recommend, is_hot, is_promote, thumb, status';
      $order = '`is_top` DESC, `sort` ASC';
      if(!isset($cid_setting['trait'])){
        $cids = implode(',', array_keys($cid_setting));
        $where = array(
            'id' => array('IN', $cids),
          //'pid' => 0
        );
        $lists = D('ItemCategory')->lists($where, 'id, name, ename,trait', 'field(id, '.$cids.') ASC');
      }else{
        $where = array(
            'trait' => array('LIKE', '%,'.$cid_setting['trait'].',%'),
        );
        $lists = D('ItemCategory')->lists($where, 'id, name, ename,trait,sort', 'field(id, sort) ASC');
      }
      
      $no_list_key=array();
      if($lists && is_array($lists)){
        foreach($lists as $key => &$value){
          $map = array(
              'cid_1|cid_2|cid_3' => $value['id'],
              'status' => array('gt', '0'), //下架商品不显示
              'is_recommend' => 1
          );
          if(C('START_POSITION') && session('zone.id')){
            $city = session('zone.district') ? session('zone.district') : session('zone.id');
            $map['_string'] = 'FIND_IN_SET("'.$city.'" , set_zone)';
          }
          $count = $item_model->where($map)->count();
          $show_num = $cid_setting[$value['id']];
          if(isset($cid_setting['num']))$show_num=$cid_setting['num'];
          //$total = ($count >= $show_num) ? $show_num : ($count - $count%4);
          $total = 40;
          $value['item_lists'] = $item_model->lists($map, $field, $order, $total);
          if(empty($value['item_lists']))$no_list_key[]=$key;
        }
      }
      foreach($no_list_key as $v)unset($lists[$v]);
      $this->lists = $lists;
    }
    $this->display('Widget/itemListsByCate');
  }
  
  
  /**
   * 调用商品列表-新的
   * @param array $cid_setting array(1=>20) 代表1分类20条数据
   * @author Max.Yu <max@jipu.com>
   * {:W('Item/itemListsByCate', array('11, 1, 3, 14, 2, 18, 10', '', '', 16))}
   */
  public function homeItemList($cid_setting = array()){
      $uid = $_SESSION['jipushop_home']['user_auth']['uid']?$_SESSION['jipushop_home']['user_auth']['uid']:0;
      $user = D('Addons://StoreSel/StoreSel')->where(array('member_id' => $uid))->find();
      $vid=Db::query("SELECT L.id FROM shante.st_user_live L INNER JOIN shante.st_user u ON u.uid=L.row_id WHERE phone<>'' && phone='{$_SESSION['jipushop_home']['user_auth']['mobile']}'")[0]['id'];
      $item_labels = I('get.item_label');
      if($user!=null&&$user['grade_id']>2&&$user['status']==1||$vid>0){
          $vizprice=1;
      }else{
          $vizprice=0;
      }
      //获取首页商品
      if($cid_setting){
  
          if(isset($cid_setting['trait'])){
              $where = array(
                      'trait' => array('LIKE', '%,'.$cid_setting['trait'].',%'),
                      'status'=> 1 ,
                  );
              $field = 'id, name, ename,trait,sort,pid';
              $order = '`sort` ASC';
              $lists = M('ItemCategory')->where($where)->field($field)->order($order)->select();
              $lists = array2tree($lists,'id','pid','child');
              $categorys = array();
              $secondCat = array();
              $firstCat = array();

              foreach ($lists as $key=>$val){
                  if( empty($val['pid'])){
                      if(!empty($val['child'])){
                          foreach ($val['child'] as $k=>$v){
                              if(!empty($v['child'])){
                                  foreach ($v['child'] as $ks=>$vs){
                                      array_push($lists[$key]['child'],$vs);
                                  }
                                  $lists[$key]['child'][$k]['child'] = array();
                              }
                          }
                      }else{
                          unset($lists[$key]);
                      }
                      
                  }else{
                      unset($lists[$key]);
                  }
              }
              
          }
          
          if($lists && is_array($lists)){
              $item_model = D('Item');
              $field = 'id, cid_1, name, subname, summary, price, mprice,zprice,gprice,ztprice,ztrate,zbyprice,dqkdh, buynum, is_top, sort, is_recommend, is_hot, is_promote, thumb, status';
              $order = '`is_top` DESC, `sort` ASC';
              foreach($lists as $key => &$value){
                  $cids = array_column($value['child'],'id');
                  array_push($cids,$value['id']);
                  $map = array(
                          'cid_1|cid_2|cid_3' => array('in',$cids),
                          'status' => array('gt', '0'), //下架商品不显示
                          'is_recommend' => 1
                  );
                  if(C('START_POSITION') && session('zone.id')){
                    $city = session('zone.district') ? session('zone.district') : session('zone.id');
                    $map['_string'] = 'FIND_IN_SET("'.$city.'" , set_zone)';
                  }
                  $count = $item_model->where($map)->count();
                  $show_num = $cid_setting[$value['id']];
                  if(isset($cid_setting['num']))$show_num=$cid_setting['num'];
                  $total = ($count >= $show_num) ? $show_num : $count;
                  $value['item_lists'] = $item_model->lists($map, $field, $order, $total);
                  if(empty($value['item_lists']))unset($lists[$key]);
              }
          }
          $this->lists = $lists;
      }
      $display = is_mobile() ? 'Widget/itemListsByCate' : 'Widget/homeItemList' ;
      $this->assign("vizprice",$vizprice);
      $this->display($display);
  }

   /**
    * 获取秒杀商品列表
    */
   public function seckillLists($setting)
   {
       if ($setting['num']) {
           $limit = $setting['num'];
           $map['status'] = 1;
           $map['stime'] = array('elt', NOW_TIME);
           $map['etime'] = array('egt', NOW_TIME);
           $field = 'id,seckill_id,item_id,item_price,item_stock,stime,etime';
           $order = 'id desc';
           $lists = M('SeckillItem')->where($map)->field($field)->order($order)->limit($limit)->select();
       }
       if($lists){
           foreach ($lists as $k => $v) {
               $item = M('Item')->where(array('id' => $v['item_id']))->find();
               $lists[$k]['name'] = $item['name'];
               $lists[$k]['thumb'] = $item['thumb'];
               $lists[$k]['cover_path'] = SITE_URL . get_cover($item['thumb'], path);
               if ($v['etime'] >= time()) {
                   $lists[$k]['remain_time'] = $v['etime'] - time();
               }
           }
           //     dump($lists);

           $this->lists = $lists;
           $display = is_mobile() ? 'Widget/itemListsByCate' : 'Widget/seckillList';
           $this->display($display);
       }
   }
}
